package com.wjx.happensbefore.volatilevariable;

import com.wjx.happensbefore.Utils;

public class VolatileRule {
    private static volatile boolean volatileStop = false;

    public static void main(String[] args) {
        Thread updater = new Thread(new Runnable() {
            @Override
            public void run() {
                Utils.sleep(1000);
                volatileStop = true;
                System.out.println("updater set stop true.");
            }
        }, "updater");

        Thread getter = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    if (volatileStop) {
                        System.out.println("getter stopped.");
                        break;
                    }
                }
            }
        }, "getter");
        updater.start();
        getter.start();
    }

}
